package cn.stylefeng.guns.modular.cms.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.cms.entity.AboutUs;
import cn.stylefeng.guns.modular.cms.mapper.AboutUsMapper;
import cn.stylefeng.guns.modular.cms.model.params.AboutUsParam;
import cn.stylefeng.guns.modular.cms.model.result.AboutUsResult;
import  cn.stylefeng.guns.modular.cms.service.AboutUsService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 关于我们表 服务实现类
 * </p>
 *
 * @author yzb
 * @since 2019-11-07
 */
@Service
public class AboutUsServiceImpl extends ServiceImpl<AboutUsMapper, AboutUs> implements AboutUsService {

    @Override
    public void add(AboutUsParam param){
        AboutUs entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(AboutUsParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(AboutUsParam param){
        AboutUs oldEntity = getOldEntity(param);
        AboutUs newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public AboutUsResult findBySpec(AboutUsParam param){
        return null;
    }

    @Override
    public List<AboutUsResult> findListBySpec(AboutUsParam param){
        return baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(AboutUsParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(AboutUsParam param){
        return param.getAboutId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private AboutUs getOldEntity(AboutUsParam param) {
        return this.getById(getKey(param));
    }

    private AboutUs getEntity(AboutUsParam param) {
        AboutUs entity = new AboutUs();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
